package com.azxcrm.project.system.order.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.azxcrm.common.utils.StringUtils;
import com.azxcrm.common.utils.security.ShiroUtils;
import com.azxcrm.framework.aspectj.lang.annotation.Log;
import com.azxcrm.framework.web.controller.BaseController;
import com.azxcrm.framework.web.domain.Message;
import com.azxcrm.framework.web.page.TableDataInfo;
import com.azxcrm.project.system.order.domain.Order;
import com.azxcrm.project.system.order.domain.OrderDeal;
import com.azxcrm.project.system.order.service.IOrderDealService;

/**
 * 成单和退单 控制层处理
 * 
 * @author Dustyone
 * @date 2018-09-11
 */
@Controller
@RequestMapping("/module/orderDeal")
public class OrderDealController extends BaseController
{
    private String prefix = "module/orderDeal";
	
	@Autowired
	private IOrderDealService orderDealService;
	
	/**
	 * 业务员角色
	 */
	@Value("${crm.role.salesman}")
	private String salesman;

	/**
	 * 客服
	 */
	@Value("${crm.role.customerService}")
	private String customerService;
	
	/**
	 * 财务
	 */
	@Value("${crm.role.treasurer}")
	private String treasurer;
	
	/**
	 * 管理员
	 */
	@Value("${crm.role.admin}")
	private String admin;
	
	@GetMapping()
	@RequiresPermissions("module:orderDeal:view")
	String orderDeal()
	{
	    return prefix + "/orderDeal";
	}
	
	/**
	 * 查询成单和退单列表
	 */
	@Log(title = "订单管理", action = "退单列表-退单列表信息")
	@GetMapping("/list")
	@RequiresPermissions("module:orderDeal:list")
	@ResponseBody
	public TableDataInfo list(OrderDeal orderDeal)
	{
		setPageInfo(orderDeal);
		Order order = new Order();
		order.setOrderStatus(6);
		orderDeal.setOrder(order);
		List<OrderDeal> list = new ArrayList<>();
		
		if(ShiroUtils.hasRole(treasurer) || ShiroUtils.hasRole(admin) ||  ShiroUtils.hasRole(customerService)){
			list = orderDealService.OrderDealList(orderDeal);
			return getDataTable(list);
		}
		
		if(ShiroUtils.hasRole(salesman)){
			order.setSalesUserId(ShiroUtils.getUserId().intValue());
			list = orderDealService.OrderDealList(orderDeal);
			return getDataTable(list);
		}
		return getDataTable(list);
	}

	/**
	 * 查询退单详情
	 */
	@Log(title = "订单管理", action = "退单列表-退单信息详情")
	@GetMapping("/edit/{orderId}")
	@RequiresPermissions("module:orderDeal:edit")
	public String edit(@PathVariable("orderId") Integer orderId, Model model)
	{
		OrderDeal orderDeal = orderDealService.selectOrderDealById(orderId);
		model.addAttribute("orderDeal", orderDeal);
		//获取成单中的图片
		String dealImages = orderDeal.getContractCertificate();
		//获取退单中的图片
		String editImages = orderDeal.getRefundsCertificate();
		List<String> dealImagesList = new ArrayList<String>();
		List<String> editImagesList = new ArrayList<String>();
		if(StringUtils.isNotEmpty(dealImages)){			
			String [] images = dealImages.split(",");
			for(int i = 0;i<images.length;i++){
				dealImagesList.add("/"+images[i]);
			}
		}
		if(StringUtils.isNotEmpty(editImages)){
			String [] images = editImages.split(",");
			for(int i = 0;i<images.length;i++){
				editImagesList.add("/"+images[i]);
			}
		}
		model.addAttribute("dealImagesList", dealImagesList);
		model.addAttribute("editImagesList", editImagesList);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存成单和退单
	 */
	@Log(title = "订单管理", action = "退单列表-保存退单信息")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("module:orderDeal:add")
	public Message save(OrderDeal orderDeal)
	{
		if (orderDealService.saveOrderDeal(orderDeal) > 0)
		{
			return Message.ok();
		}
		return Message.error();
	}
	
	/**
	 * 删除成单和退单
	 */
	@Log(title = "订单管理", action = "退单列表-删除退单信息")
	@PostMapping( "/remove/{dealId}")
	@ResponseBody
	@RequiresPermissions("module:orderDeal:remove")
	public Message remove(@PathVariable("dealId") Integer dealId)
	{
		if (orderDealService.deleteOrderDealById(dealId) > 0)
		{
		    return Message.ok();
		}
		return Message.error();
	}
	
	/**
	 * 批量删除成单和退单
	 */
	@Log(title = "订单管理", action = "退单列表-批量删除退单信息")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("module:orderDeal:batchRemove")
	public Message remove(@RequestParam("ids[]") Integer[] dealIds)
	{
		int rows = orderDealService.batchDeleteOrderDeal(dealIds);
		if (rows > 0)
        {
            return Message.ok();
        }
        return Message.error();
	}
	
}
